//
//  KZFreeProductCell.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/27.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

class KZFreeProductCell: UITableViewCell {
    
    let iconView = UIImageView()
    let nameLabel = UILabel()       // 名字
    let priceLabel = UILabel()      // 现价
    let oldPriceLabel = UILabel()   // 原价
    let codeNoLabel = UILabel()     // 订单号
    
    let statusLabel = UILabel()     // 状态
    let funcButton = UIButton()     // 功能按钮
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        self.backgroundColor = UIColor.clear
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}

// MARK: ------ 创建ui
extension KZFreeProductCell {
    
    private func setupUI() {
        
        
        let bgView = UIView()
        bgView.backgroundColor = UIColor.white
        contentView.addSubview(bgView)
        bgView.snp.makeConstraints { (m) in
            m.left.equalTo(W(5))
            m.right.equalTo(-W(5))
            
            if kIsIPhoneX {
                m.height.equalTo(106)
            } else {
                m.height.equalTo(H(106))
            }
            
            m.top.equalTo(0)
            m.bottom.equalTo(0)
        }
        
        
        bgView.addSubview(iconView)
        iconView.image = kDefaultImage
        iconView.snp.makeConstraints { (m) in
            m.left.equalTo(W(10))
            m.top.equalTo(H(10))
            m.width.equalTo(W(85))
            m.height.equalTo(iconView.snp.width)
            
        }
        
        funcButton.addTarget(self, action: #selector(onButtonClick(button:)), for: .touchUpInside)
        funcButton.backgroundColor = kThemeColor
        funcButton.layer.cornerRadius = 2
        funcButton.layer.masksToBounds = true
        funcButton.setTitle("提交订单号", for: .normal)
        funcButton.setTitleColor(UIColor.white, for: .normal)
        funcButton.titleLabel?.font = kFont(10)
        bgView.addSubview(funcButton)
        funcButton.snp.makeConstraints { (m) in
            m.width.equalTo(W(75))
            m.height.equalTo(funcButton.snp.width).multipliedBy(20.0/75.0)
            m.right.equalTo(-W(10))
            m.centerY.equalTo(bgView)
        }
        
        statusLabel.textColor = kThemeColor
        statusLabel.numberOfLines = 2
        statusLabel.textAlignment = .center
        statusLabel.text = "未提交订单号"
        statusLabel.font = UIFont.systemFont(ofSize: 10)
        bgView.addSubview(statusLabel)
        statusLabel.snp.makeConstraints { (m) in
            m.centerX.equalTo(funcButton)
            m.bottom.equalTo(funcButton.snp.top).offset(-5)
        }
        
        nameLabel.textColor = UIColor.black
        nameLabel.numberOfLines = 2
        nameLabel.textAlignment = .left
        nameLabel.text = "智能心率手环、手表(连接手机)qqqqqqqqqqqqq"
        nameLabel.font = UIFont.systemFont(ofSize: 14)
        bgView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (m) in
            m.left.equalTo(iconView.snp.right).offset(W(10))
            m.right.equalTo(funcButton.snp.left).offset(-W(10))
            m.top.equalTo(iconView)
        }
        
        
        priceLabel.textColor = kThemeColor
        priceLabel.textAlignment = .left
        priceLabel.text = "￥125.0"
        priceLabel.font = UIFont.systemFont(ofSize: 16)
        bgView.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { (m) in
            m.left.equalTo(nameLabel)
            m.height.equalTo(20)
            m.top.equalTo(nameLabel.snp.bottom).offset(H(5))
        }
        
        oldPriceLabel.textColor = UIColor.lightGray
        oldPriceLabel.textAlignment = .left
        oldPriceLabel.text = "￥188.0"
        oldPriceLabel.font = UIFont.systemFont(ofSize: 12)
        bgView.addSubview(oldPriceLabel)
        oldPriceLabel.snp.makeConstraints { (m) in
            m.left.equalTo(priceLabel.snp.right).offset(5)
            m.bottom.height.equalTo(priceLabel)
        }
        oldPriceLabel.attributedText = oldPriceLabel.text?.attrubutesString(attrubutes: [NSAttributedStringKey.strikethroughStyle: 2])
        
        codeNoLabel.textColor = UIColor.gray
        codeNoLabel.adjustsFontSizeToFitWidth = true
        codeNoLabel.textAlignment = .left
        codeNoLabel.text = "订单号12432423482038420"
        codeNoLabel.font = UIFont.systemFont(ofSize: 10)
        bgView.addSubview(codeNoLabel)
        codeNoLabel.snp.makeConstraints { (m) in
            m.left.right.equalTo(nameLabel)
            m.height.equalTo(15)
            m.top.equalTo(oldPriceLabel.snp.bottom).offset(H(5))
        }

    }
}

extension KZFreeProductCell {
    
    @objc func onButtonClick(button: UIButton) {
        
        KZCodeSubmitView().show()
    }
}
